package com.gzhu.knowledgeAdmin.controller;



import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.gzhu.knowledgeAdmin.common.model.Option;
import com.gzhu.knowledgeAdmin.common.result.ErrorCode;
import com.gzhu.knowledgeAdmin.common.result.R;
import com.gzhu.knowledgeAdmin.common.vo.DoAssignVo;
import com.gzhu.knowledgeAdmin.entity.Permission;
import com.gzhu.knowledgeAdmin.security.TokenManager;
import com.gzhu.knowledgeAdmin.service.PermissionService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 权限 菜单管理
 * </p>
 *
 * @author testjava
 * @since 2020-01-12
 */
@RestController
@RequestMapping("/admin/acl/permission")
//@CrossOrigin
public class PermissionController {

    @Autowired
    private PermissionService permissionService;
    @Autowired
    private TokenManager tokenManager;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     *菜单下拉列表
     * @return
     */
    @GetMapping("/options")
    public R listMenuOptions() {
        List<Option> menus = permissionService.listPermissionOptions();
        return R.ok(menus);
    }


    @ApiOperation(value = "根据id获取菜单")
    @GetMapping("get/{id}")
    @PreAuthorize("hasAuthority('permission.update')")
    public R get(@PathVariable String id) {
        Permission permission = permissionService.getById(id);
        return R.ok(permission);
    }
    // 获取权限
    @ApiOperation(value = "获取用户所有权限值")
    @GetMapping("permissionList")
    public R getPermissionList(HttpServletRequest request){
        String token = request.getHeader("token");
        if (token != null && !"".equals(token.trim())) {
            String userName = tokenManager.getUserFromToken(token);

            String permissionValueListJson = stringRedisTemplate.opsForValue().get(userName);
            List<String> permissionValueList = JSON.parseObject(permissionValueListJson,new TypeReference<List<String>>(){}) ;
            return R.ok(permissionValueList);
        }
        return R.error(ErrorCode.NOT_LOGIN_ERROR);
    }


    //获取全部菜单
    @ApiOperation(value = "查询所有菜单")
    @GetMapping
    @PreAuthorize("hasAuthority('permission.list')")
    public R indexAllPermission() {
        List<Permission> list =  permissionService.queryAllMenuGuli();
        return R.ok(list);
    }

    @ApiOperation(value = "递归删除菜单")
    @DeleteMapping("remove/{id}")
    @PreAuthorize("hasAuthority('permission.remove')")
    public R remove(@PathVariable String id) {
        permissionService.removeChildByIdGuli(id);
        return R.ok();
    }

    @ApiOperation(value = "给角色分配权限")
    @PostMapping("/doAssign")
    @PreAuthorize("hasAuthority('role.acl')")
    public R doAssign(@RequestBody DoAssignVo doAssignVo) {
        permissionService.saveRolePermissionRealtionShip(doAssignVo);
        return R.ok();
    }


    @ApiOperation(value = "新增菜单")
    @PostMapping("save")
    @PreAuthorize("hasAuthority('permission.add')")
    public R save(@RequestBody Permission permission) {
        permissionService.save(permission);
        return R.ok();
    }

    @ApiOperation(value = "修改菜单")
    @PutMapping("update")
    @PreAuthorize("hasAuthority('permission.update')")
    public R updateById(@RequestBody Permission permission) {
        permissionService.updatePermissionById(permission);
        return R.ok();
    }

}

